<!-- 结算详情 -->
<template>
    <page-header-wrapper class="detail" @back="() => $router.go(-1)">
        <template v-slot:title>
            <div class="page-header-title" style="width: 100%">
                <div class="flex spaceBetween alignCenter" style="width: 100%">
                    <div>结算单名称：{{settlement.settlementName}}</div>
                    <div>
                        <a-button v-if="settlement.state == 0 && settlement.isCustomerCreate == '1'" type='' style="margin: 0 10px" @click="cancelOrder">取消</a-button>
                        <a-button v-if="settlement.state == 1 && hasPower('settlementPay')" type='primary' style="margin: 0 10px" @click="pay">去支付</a-button>
                    </div>
                </div>
            </div>
        </template>
        <template v-slot:content>
            <div class="page-header-content">
                <div class="content">
                    <div class="flex spaceBetween">
                        <div>
                            <div>上传时间：{{settlement.createTime}}</div>
                        </div>
                        <div class="flex">
                            <div class="flex flexColumn flexCenter" style="margin-right: 30px">
                                <div>状态</div>
                                <div style="font-size: 16px;font-weight: 500;color: #333">{{settlement.stateDesc}}</div>
                            </div>
                            <div class="flex flexColumn flexCenter" style="margin-right: 30px">
                                <div>明细笔数</div>
                                <div style="font-size: 16px;font-weight: 500;color: #333">{{settlement.detailedCount}}</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </template>

        <!-- 订单详情 -->
        <a-card style="margin-top: 24px" :bordered="false" title="订单详情">
            <a-descriptions>
                <a-descriptions-item label="订单号">{{settlement.settlementNo}}</a-descriptions-item>
                <a-descriptions-item label="所属公司">{{settlement.companyName}}</a-descriptions-item>
                <a-descriptions-item label="所属客户">{{settlement.customerName}}</a-descriptions-item>
                <a-descriptions-item label="项目名称">{{settlement.projectName}}</a-descriptions-item>
                <a-descriptions-item label="驳回时间" v-if="settlement.state == 5">{{settlement.refuseTime}}</a-descriptions-item>
                <a-descriptions-item label="驳回原因" v-if="settlement.state == 5">{{settlement.refuseReason}}</a-descriptions-item>
            </a-descriptions>
            <!-- 付款账户信息 -->
            <a-descriptions title="付款账户信息" v-if="(settlement.state == 2 || settlement.state == 3 || settlement.state == 4 || settlement.state == 7 || settlement.state == 8) && settlement.settlementPayment">
                <a-descriptions-item label="账户名称">{{settlement.settlementPayment.accountName}}</a-descriptions-item>
                <a-descriptions-item label="银行账号">{{settlement.settlementPayment.bankAccount}}</a-descriptions-item>
                <a-descriptions-item label="开户行">{{settlement.settlementPayment.channel}}</a-descriptions-item>
                <a-descriptions-item label="联动设置">{{settlement.settlementPayment.linkageType | linkageTypeFilter}}</a-descriptions-item>
                <a-descriptions-item label="账户类型">{{settlement.settlementPayment.accountType | accountTypeFilter}}</a-descriptions-item>
            </a-descriptions>
        </a-card>

        <a-card style="margin-top: 20px" :bordered="false" v-if="settlement.state == 3 || settlement.state == 4 || settlement.state == 7 || settlement.state == 8">
            <a-row>
                <a-col :sm="8" :xs="24">
                    <Info title="服务费率" :value="`${settlement.serviceRate}%`"/>
                </a-col>
                <a-col :sm="8" :xs="24">
                    <Info title="订单总金额" :value="`￥${settlement.totalAmount}`"/>
                </a-col>
                <a-col :sm="8" :xs="24">
                    <Info title="结算金额" :value="`￥${settlement.amount}`" :rightBordered="false" />
                </a-col>
            </a-row>
            <a-row>
                <a-col :sm="8" :xs="24">
                    <Info title="服务费" :value="`￥${settlement.serviceAmount}`" :btmBordered="false"/>
                </a-col>
                <a-col :sm="8" :xs="24">
                    <Info title="发放成功金额" :value="`￥${settlement.successAmount}（成功人数：${settlement.successCount}人）`" :btmBordered="false"/>
                </a-col>
                <a-col :sm="8" :xs="24">
                    <Info title="发放失败金额" :value="`￥${settlement.failAmount}（失败人数：${settlement.failCount}人）`" :rightBordered="false" :btmBordered="false"/>
                </a-col>
            </a-row>
        </a-card>
        <a-card v-else style="margin-top: 20px" :bordered="false">
            <a-row>
                <a-col :sm="4" :xs="24">
                    <div style="margin-bottom: 30px">服务费率：</div>
                    <a href="javascript:void(0)">{{settlement.serviceRate}}%</a>
                </a-col>
                <a-col :sm="2" :xs="24">
                    <a-divider type="vertical" style="height: 70px"/>
                </a-col>
                <a-col :sm="18" :xs="24">
                    <a-row>
                        <a-col :sm="5" :xs="24">
                            <div style="margin-bottom: 30px">订单总金额：</div>
                            <div>￥{{settlement.totalAmount}}</div>
                        </a-col>
                        <a-col :sm="5" :xs="24">
                            <div style="margin-bottom: 30px">结算金额：</div>
                            <div>￥{{settlement.amount}}</div>
                        </a-col>
                        <a-col :sm="5" :xs="24">
                            <div style="margin-bottom: 30px">服务费：</div>
                            <div>￥{{settlement.serviceAmount}}</div>
                        </a-col>
                    </a-row>
                </a-col>
            </a-row>
        </a-card>

        <!-- 结算明细 -->
        <a-card style="margin-top: 20px" :bordered="false">
            <a-tabs v-model="active">
                <a-tab-pane :key="1" tab="结算名单">
                    <detailList :detailType="1" :settlementId="settlementId"/>
                </a-tab-pane>
                <!-- <a-tab-pane :key="2" tab="民工清单">
                    <detailList :detailType="2" :settlementId="settlementId"/>
                </a-tab-pane>
                <a-upload
                    slot="tabBarExtraContent"
                    style="display: inline-block"
                    :before-upload="beforeUpload"
                    @change="handleChange"
                    :fileList="[]"
                >
                    <a-button type="primary"> <a-icon type="upload" />上传民工清单</a-button>
                </a-upload> -->
            </a-tabs>
        </a-card>

        <!-- 拒绝原因 -->
        <a-modal 
            v-model="visible" 
            title="拒绝原因"
            @ok="handleOk" 
            @cancel="visible = false">
            <div>
                <a-form-model :model="refuse" ref="ruleForm" :rules="rules" :label-col="{ span: 8 }" :wrapper-col="{ span: 15 }">
                    <a-form-model-item label="拒绝原因" prop="refuseReason">
                        <a-textarea v-model="refuse.refuseReason" placeholder="请输入拒绝原因" :auto-size="{ minRows: 4, maxRows: 6 }" allow-clear/>
                    </a-form-model-item>
                </a-form-model>
            </div>
        </a-modal>

        <!-- 去支付 -->
        <a-modal 
            v-model="payVisible" 
            title="确认支付"
            @ok="payHandleOk" 
            width="75%"
            @cancel="payVisible = false">
            <div>
                <a-descriptions :title="`结算金额${settlement.amount}`">
                    <a-descriptions-item label="订单总金额">{{settlement.totalAmount}}</a-descriptions-item>
                    <a-descriptions-item label="服务费">{{settlement.serviceAmount}}</a-descriptions-item>
                    <a-descriptions-item label="服务费率">{{settlement.serviceRate}}%</a-descriptions-item>
                    <a-descriptions-item label="结算单名称">{{settlement.settlementName}}</a-descriptions-item>
                    <a-descriptions-item label="所属公司">{{settlement.companyName}}</a-descriptions-item>
                    <a-descriptions-item label="所属客户">{{settlement.customerName}}</a-descriptions-item>
                    <a-descriptions-item label="明细笔数">{{settlement.detailedCount}}</a-descriptions-item>
                    <a-descriptions-item label="项目名称">{{settlement.projectName}}</a-descriptions-item>
                </a-descriptions>
                <a-form-model v-if="needPayPassword && hasPayPassword" :label-col="{ span:3 }" :wrapper-col="{ span: 15 }">
                    <a-form-model-item label="支付密码" labelAlign="left">
                        <a-input v-model="payPassword" placeholder="请输入支付密码" allow-clear/>
                    </a-form-model-item>
                </a-form-model>
                <a-divider/>
                <div>可用支付账户</div>
                <a-radio-group v-model="accountId" @change="selAccount">
                    <a-radio :style="radioStyle" v-for="account in accountList" :key="account.id" :value="account.id">账户名称：{{account.accountName}}({{account.bankAccount}})，账户余额：{{account.amount}}，联动设置：{{account.linkageType |linkageTypeFilter}}</a-radio>
                </a-radio-group>
            </div>
        </a-modal>
    </page-header-wrapper>
</template>

<script>
import { getSettlementDetail , uploadSettlementExcel , cancelSettlementOrder , passSettlementOrder , refuseSettlementOrder , authSettlementOrder , settlementPay } from '@/api/services/settlement';
import { getAccountBySettlement , getHasPayPassword } from '@/api/services/account';
import Info from '../../../Services/Statistics/components/Info';
import detailList from './components/detailList';
export default {
    components: {
        Info,
        detailList
    },
    data () {
        return {
            settlement: {
                settlementName: '',
                state: '0',
            },
            settlementId: '',
            active: 1,
            rules: {
                refuseReason: [{ required: true, message: '请输入拒绝原因', trigger: 'blur' }]
            },
            refuse: {
                refuseReason: '',
            },
            visible: false,
            payVisible: false,
            accountList: [], // 已关联账户列表
            accountId: undefined,
            radioStyle: {
                display: 'block',
                height: '30px',
                lineHeight: '30px',
            },
            payPassword: '',
            hasPayPassword: false,
            needPayPassword: false,
        };
    },
    created(){
        this.settlementId = this.$route.query.settlementId;
        this.getDetail();
    },
    methods: {
        // 获取结算单详情
        getDetail(){
            let _this = this;
            getSettlementDetail(_this.settlementId)
            .then(res=> {
                if(res.success){
                    _this.settlement = res.data;
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
        // 上传民工清单
        beforeUpload(file) {
            return false;
        },
        handleChange({file,fileList}){
            let _this = this
            const formData = new FormData();
            formData.append('file',file)
            formData.append('settlementId',_this.settlementId)
            uploadSettlementExcel(formData)
            .then(res=>{
                if(res.success){
                    _this.$router.push({path: '/companysSettlement/detail/excelUpload', query: { 'fileData': JSON.stringify(res.data), 'settlementId': _this.settlementId}})
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
        // 取消结算单
        cancelOrder(){
            let _this = this;
            _this.$confirm({
                title: '温馨提示',
                content: '是否确认取消当前结算单？',
                onOk(){
                    cancelSettlementOrder(_this.settlementId)
                    .then(res=> {
                        if(res.success){
                            _this.$notification['success']({
                                message: '温馨提示',
                                description: '取消成功',
                                duration: 8
                            })
                            _this.getDetail();
                        }else{
                            _this.$notification['error']({
                                message: '温馨提示',
                                description: res.errMessage,
                                duration: 8
                            })
                        }
                    })
                }
            })
        },
        // 通过结算单
        passOrder(){
            let _this = this;
            _this.$confirm({
                title: '温馨提示',
                content: '是否确认通过当前结算单？',
                onOk(){
                    passSettlementOrder(_this.settlementId)
                    .then(res=> {
                        if(res.success){
                            _this.$notification['success']({
                                message: '温馨提示',
                                description: '通过成功',
                                duration: 8
                            })
                            _this.getDetail();
                        }else{
                            _this.$notification['error']({
                                message: '温馨提示',
                                description: res.errMessage,
                                duration: 8
                            })
                        }
                    })
                }
            })
        },
        // 拒绝结算单
        handleOk(){
            let _this = this
            _this.$refs.ruleForm.validate(valid => {
                if(valid){
                    const params = {
                        'settlementId': _this.settlementId,
                        'refuseReason': _this.refuse.refuseReason
                    }
                    refuseSettlementOrder(params)
                    .then(res=>{
                        if(res.success){
                            _this.visible = false;
                            _this.getDetail();
                            _this.$notification['success']({
                                message: '温馨提示',
                                description: '操作成功',
                                duration: 8
                            })
                        }else{
                            _this.$notification['error']({
                                message: '温馨提示',
                                description: res.errMessage,
                                duration: 8
                            })
                        }
                    })
                }
            })
        },
        // 授权结算单
        authOrder(){
            let _this = this;
            _this.$confirm({
                title: '温馨提示',
                content: '是否确认通过当前结算单？',
                onOk(){
                    authSettlementOrder(_this.settlementId)
                    .then(res=> {
                        if(res.success){
                            _this.$notification['success']({
                                message: '温馨提示',
                                description: '通过成功',
                                duration: 8
                            })
                            _this.getDetail();
                        }else{
                            _this.$notification['error']({
                                message: '温馨提示',
                                description: res.errMessage,
                                duration: 8
                            })
                        }
                    })
                }
            })
        },
        // 去支付
        pay(){
            let _this = this;
            _this.accountId = undefined;
            _this.accountList = [];
            getAccountBySettlement(_this.settlementId)
            .then(res => {
                if(res.success){
                    _this.accountList = res.data
                    _this.payVisible = true;
                    if(res.data.length > 0){
                        _this.accountId = res.data[0].id
                        if(res.data[0].needDealPassword == '1'){
                            _this.needPayPassword = true;
                        }else{
                            _this.needPayPassword = false;
                        }
                    }
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
        // 确认支付
        payHandleOk(){
            let _this = this;
            if(_this.accountId == undefined){
                _this.$message.warning('请先与该结算单客户绑定业务账号，再进行支付操作！');
                return;
            }
            if(_this.accountId){
                let account = _this.accountList.find(item => item.id == _this.accountId);
                if(account.amount < _this.settlement.amount){
                    _this.$message.warning('当前账户余额不足！');
                    return;
                }
            }
            if(_this.hasPayPassword && _this.needPayPassword){
                if(_this.payPassword == ''){
                    _this.$message.warning('请输入交易密码！');
                    return;
                }
            }
            const params = {
                'settlementId': _this.settlementId,
                'accountId': _this.accountId,
                'payPassword': _this.payPassword,
            }
            settlementPay(params)
            .then(res => {
                if(res.success){
                    _this.$notification['success']({
                        message: '温馨提示',
                        description: '已提交支付',
                        duration: 8
                    })
                    _this.payVisible = false
                    _this.getDetail();
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
        // 获取是否设置过交易密码
        getHasPaypassword(accountId){
            let _this = this
            getHasPayPassword(accountId)
            .then(res=>{
                if(res.success){
                    _this.hasPayPassword = res.data;
                }else{
                    _this.$notification['error']({
                        message: '温馨提示',
                        description: res.errMessage,
                        duration: 8
                    })
                }
            })
        },
        selAccount(e){
            this.getHasPaypassword(e.target.value)
            var account = this.accountList.find(item => item.id == e.target.value);
            if(account.needDealPassword == '1'){
                this.needPayPassword = true;
            }else{
                this.needPayPassword = false;
            }
        }
    }
}

</script>
<style scoped>
.detail /deep/ .ant-page-header-heading-title{
    float: none;
}
</style>